package com.avit.tv.kui.pro;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.avit.tv.kui.R;
import com.avit.tv.kui.base.BaseFocusLayout;
import com.avit.tv.kui.pro.volume.VolumeListener;
import com.avit.tv.kui.pro.volume.VolumeProgressView;
import com.avit.tv.kui.utils.Utils;

/**
 * 归档并重写音量条控件
 *
 * @author like
 * @date 2020/8/28
 */
public class AvitVolumeView extends BaseFocusLayout implements VolumeListener {
    private int iconColor;
    private int progressColor;
    private int iconSize;
    private ImageView icon;
    private TextView volumeNum;
    private VolumeProgressView volumeProgress;
    private int progressWidth;

    public AvitVolumeView(@NonNull Context context) {
        super(context);
    }

    public AvitVolumeView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public AvitVolumeView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.AvitVolumeView, defStyleAttr, 0);
        iconColor = typedArray.getColor(R.styleable.AvitVolumeView_icon_color, Color.WHITE);
        progressColor = typedArray.getColor(R.styleable.AvitVolumeView_volume_progress_color, Color.WHITE);
        iconSize = typedArray.getDimensionPixelSize(R.styleable.AvitVolumeView_icon_size, DEFAULT_SIZE);
        typedArray.recycle();

    }

    @Override
    protected void initContent(Context context) {
        setBackgroundColor(Color.BLACK);
        icon = new ImageView(context);
        volumeNum = new TextView(context);
        volumeNum.setTextColor(iconColor);
        volumeNum.setTextSize(TypedValue.COMPLEX_UNIT_PX, iconSize);
        volumeProgress = new VolumeProgressView(context);
        volumeProgress.setMaxHeight(iconSize);
        volumeProgress.setProgressColor(progressColor);
        progressWidth = Utils.getRadix(getContext()) * 120;
        Utils.setSvgImageRes(icon, context.getResources().getDrawable(R.drawable.ic_volume), iconColor);
    }

    @Override
    protected void insertView() {
        LayoutParams iconParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        iconParams.width = iconSize;
        iconParams.height = iconSize;
        iconParams.leftMargin = DEFAULT_SIZE;
        iconParams.gravity = Gravity.START | Gravity.CENTER_VERTICAL;
        addView(icon, iconParams);
        LayoutParams progressParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        progressParams.width = progressWidth;
        progressParams.gravity = Gravity.CENTER;
        progressParams.rightMargin = 2 * DEFAULT_SIZE + iconSize;
        progressParams.leftMargin = 2 * DEFAULT_SIZE + iconSize;
        addView(volumeProgress, progressParams);
        LayoutParams numParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        numParams.width = LayoutParams.WRAP_CONTENT;
        numParams.gravity = Gravity.END | Gravity.CENTER_VERTICAL;
        numParams.rightMargin = DEFAULT_SIZE;
        addView(volumeNum, numParams);
        loseFocus(true);
    }


    /**
     * 设置icon 颜色
     *
     * @param iconColor
     */
    @Override
    public void setIconColor(int iconColor) {
        this.iconColor = iconColor;
        Utils.setSvgImageRes(icon, getContext().getResources().getDrawable(R.drawable.ic_volume), iconColor);
    }

    /**
     * 设置进度条颜色
     *
     * @param progressColor
     */
    @Override
    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        volumeProgress.setProgressColor(progressColor);
    }

    /**
     * 设置图标大小
     *
     * @param iconSize
     */
    @Override
    public void setIconSize(int iconSize) {
        this.iconSize = iconSize;
        icon.getLayoutParams().width = iconSize;
        icon.getLayoutParams().height = iconSize;
    }

    /**
     * 获取进度从而显示
     *
     * @return
     */
    public VolumeProgressView getVolumeProgress() {
        return volumeProgress;
    }

    /**
     * 音量进度
     *
     * @param progressValue
     */
    @SuppressLint("SetTextI18n")
    @Override
    public void onProgress(int progressValue) {
        volumeNum.setText(progressValue + "");
    }

    /**
     * 监听结果返回为当前进度
     *
     * @param currentValue
     */
    @Override
    public void onCurrentVolume(int currentValue,int maxValue) {
        if (currentValue == maxValue)
            volumeProgress.setCurrentX(progressWidth);
        else if (currentValue == 0)
            volumeProgress.setCurrentX(0);
        else {
            float currentX = currentValue * volumeProgress.getMoveX();
            volumeProgress.setCurrentX(currentX);
        }
    }

    /**
     * 移动的值
     *
     * @param maxValue
     */
    @Override
    public void setMoveValue(int maxValue) {
        volumeProgress.setMoveX((progressWidth-DEFAULT_SIZE)*1f/ maxValue);
    }

    /**
     * 是否是音量增加
     *
     * @param isUp
     */
    @Override
    public void isProgressUp(boolean isUp) {
        if (isUp)
            volumeProgress.up();
        else
            volumeProgress.down();
    }

    @Override
    public AvitVolumeView getVolumeView() {
        return this;
    }


}
